<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Leaverequest extends PNC_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model(array("mod_leave_request", "mod_record", "mod_option"));
	}

	/*
	 * Index
	 * @access public
	 * @return void
	 */

	public function index() {
		redirect('leaverequest/do_request/');
	}

	/* Do request for leaving form 
	 * Add new record
	 */

	public function do_request() {
		if ($this->check_session()) {
			$data['title'] = "Do Request";
			$data['validator'] = $this->mod_option->get_manage_mail('manager', $this->session->userdata('staff_id'));
			$data['super'] = $this->mod_leave_request->super_type_leave();
			$data['type'] = $this->mod_leave_request->type_leave();
			$data['sub_type'] = $this->mod_leave_request->sub_type_leave();
			$data['sub_type_per'] = $this->mod_leave_request->sub_type_leave_per();
			if ($this->input->post('btn_do_request')) {
				$config = array(
					array(
						'field' => 'duration',
						'label' => 'Time Duration',
						'rules' => 'trim|required|xss_clean'
					),
					array(
						'field' => 'reason',
						'label' => 'Reason',
						'rules' => 'trim|required|xss_clean'
					),
					array(
						'field' => 'super_type',
						'label' => 'Type of Leave',
						'rules' => 'trim|required|greater_than[0]'
					),
					array(
						'field' => 'validators',
						'label' => 'Manager Mail',
						'rules' => 'trim|required|valid_email'
					),
					array(
						'field' => 's_date',
						'label' => ' Start Date',
						'rules' => 'trim|required|callback_compareDates'
					),
					array(
						'field' => 'e_date',
						'label' => 'End Date',
						'rules' => 'trim|required|callback_compareDates'
					)
				);
				$this->form_validation->set_rules($config);
				$this->form_validation->set_message('greater_than[0]', 'The field must be select.');
				if ($this->form_validation->run() === FALSE) {
					$this->session->set_userdata('validate_fail', show_message('Please check your completed form!', 'error'));
					$this->view_main($data);
				} else {
					$staff = $this->session->userdata('staff_id');
					$validator = $this->input->post('validators');
					$super_type = $this->input->post('super_type');
					$sub_type = $this->input->post('sub');
					$sub_sub = $this->input->post('sub_sub');
					$desc = $this->input->post('desc_up');
					$work_on = $this->input->post('working_on');
					$catch_on = $this->input->post('catchup_on');
					$reason = $this->input->post('reason');
					$start_date = $this->input->post('s_date');
					$end_date = $this->input->post('e_date');
					$hour = $this->input->post('duration');

					$this->session->set_userdata('reciever_mail', $validator);
					$this->session->set_userdata('reason_leaving', $reason);

					$day = ($hour / 8);

					if ($start_date <= $end_date) {
						$success_request = '';
						if ($super_type == 1) {
							$success_request = PNC_Model::add_record($staff, $validator, $super_type, $sub_type, $sub_sub, '', $reason, $start_date, $end_date, $hour, $day, '', '');
							if ($success_request > 0) {
								$this->send_mail_request('record', 'leaverequest', 'show_record', 'do_request');
							} else {
								$this->session->set_userdata('sending_erro', show_message('Can not do request leaving!', 'error'));
								$this->view_main($data);
							}
						} else if ($super_type == 2) {
							$success_request = PNC_Model::add_record($staff, $validator, $super_type, '', '', $desc, $reason, $start_date, $end_date, $hour, $day, $work_on, $catch_on);
							if ($success_request > 0) {
								$this->send_mail_request('record', 'leaverequest', 'show_record', 'do_request');
							} else {
								$this->session->set_userdata('sending_erro', show_message('Can not do request leaving!', 'error'));
								$this->view_main($data);
							}
						}
					} else {
						$this->session->set_userdata('sent_erro', show_message('Start date must be smaller than end date!', 'error'));
						$this->view_main($data);
					}
				}
			} else {
				$this->view_main($data);
			}
		}
	}

}

/* End of file leaverequest.php */
	/* Location: ./application/controllers/leaverequest.php */